package com.nextive.weightlogger.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import com.nextive.weightlogger.R;

/**
 * Utility class to simplify showing dialogs.
 * 
 * @author Federico Baseggio <fedebaseggio@gmail.com>
 */
public class DialogUtil {

	/**
	 * Shows a dialog with the specified message and a dismiss button.
	 * 
	 * @param context
	 *            The context to launch this dialog in, usually an activity.
	 * @param messageStringResource
	 *            The string resource id of the message to display.
	 */
	public static void showMessage(Context context, int messageStringResource)
	{
		showMessage(context, messageStringResource, null);
	}
	
	/**
	 * Shows a dialog with the specified message and a dismiss button, and sets
	 * focus on the specified view when dismissed.
	 * 
	 * @param context
	 *            The context to launch this dialog in, usually an activity.
	 * @param messageStringResource
	 *            The string resource id of the message to display.
	 * @param viewInvolved
	 *            The view to set focus on when the dialog is dismissed. If
	 *            null, this parameter is ignored.
	 */
	public static void showMessage(Context context, int messageStringResource, final View viewInvolved)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		
		builder.setMessage(messageStringResource);
		builder.setPositiveButton(R.string.dialog_ok, 
				new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (viewInvolved != null)
				{
					viewInvolved.requestFocus();
				}
			}
		});
		
		AlertDialog alert = builder.create();
		alert.show();
	}
}
